#include <QDebug>
#include "gui/stockview.h"
#include "gui/stockviewadddialog.h"

void StockView::onDataUpdate(QList<BaseModel *> models)
{
    StockModel *stock;
    ShinyListItem *listItem;
    int i;

    qDebug() << "StockView: Update received";
    BaseView::onDataUpdate(models);

    this->slist->clear();
    for (i = 0; i < models.length(); i++) {
        stock = (StockModel *) models.at(i);
        listItem = new ShinyListItem;
        listItem->setItemIcon("res/stock.png");

        listItem->setMainText(stock->getSerial());
        listItem->setSubText("Reserved");
        this->slist->addItem(listItem);
    }
}

void StockView::onDetailsRequested(BaseModel *model)
{
    // Do we even want something to pop up here?
    // StockModel *stock = (StockModel *) model;
}

void StockView::onAddButtonClicked()
{
    StockModel *stock;
    StockController *ctrl = (StockController *) this->getController();
    StockViewAddDialog *dialog = new StockViewAddDialog(this, this->item);
    int result = dialog->exec();

    if (result == QDialog::Accepted) {
        stock = dialog->getStock();
        ctrl->addStock(stock);
        ctrl->getStocksOf(this->item); // update the display
    }

    delete dialog;
}

void StockView::initViewComponents()
{
    BaseView::initViewComponents();

    this->addButton->setText("Add Stock");
    this->hideReportsWidgets();
}

void StockView::initViewSignalsAndSlots()
{
    BaseView::initViewSignalsAndSlots();
    connect(this, SIGNAL(detailsRequested(BaseModel*)),
            this, SLOT(onDetailsRequested(BaseModel*)));
    connect(this->addButton, SIGNAL(clicked()),
            this, SLOT(onAddButtonClicked()));
}

StockView::StockView(QWidget *parent, ItemModel *item) :
    BaseView(parent)
{
    this->item = item;
    this->setMinimumSize(500, 300);
    this->initViewComponents();
    this->initViewSignalsAndSlots();
}
